var app = new Vue({
	el: '#app',
	data: {
		isPhoneE: false, //手机号是否已注册
		code_tips: '获取验证码',
		time: '',
		tips: '',
		err: false,
		info: {
			UserName: '',
			Mobile: '',
			Email: '',
			Password: '',
			Password_2: '',
			ValidateCode: ''
		},
		infos:{
			user:globalData.userInfo
		},//用户的所有信息
		url: {
			registerUser: '/api/UserInfo/RegisterUser',
			getRegCode: '/api/Home/CheckMobileMsgCode',
		},
		code_info:{
			time: '',
			code_tips: '获取验证码',
			intervals:60,
			get_code:'获取验证码',
			wait_code:'秒后获取',
			server_code:"",//服务器返回的验证码
		}
	},
	computed: {
		isCode: function() {
			var tel = /^1\d{10}$/
			return(tel.test(this.info.Mobile) && this.code_info.time == '')
		}
	},
	mounted: function() {
		globalData.getTempToken(function(){
			//隐藏loading
			$('.loading').hide()			
		})
		setTimeout(function(){
			$('#register').bootstrapValidator(register_valid_json);
		},100)
		
	},
	methods: {
		getCode: function() {
			var that = this;
			if(!that.isCode)
				return;
			//开始倒计时
			that.code_info.time = this.code_info.intervals
			that.isCode = false
			that.code_info.code_tips = that.code_info.wait_code			
			var timer = setInterval(function() {
				that.code_info.time--
					if(that.code_info.time < 1) {
						clearInterval(timer);
						that.code_info.time = ''
						that.isCode = true
						that.code_info.code_tips = that.code_info.get_code
					}
			}, 1000)
			//判断用户是否存在,不存在会返回验证码
			globalData.ajaxPost(that.url.getRegCode, {
				"Mobile": this.info.Mobile,
				"MsgType": 1
			}, function(res) {
				console.log(res)
				if(!res.suc){
					that.isPhoneE = true;
					return;
				}else{
					//获取返回的验证码
//					that.code_info.server_code = res.data
				}
				
			})
		},
		//注册
		register: function() {
			var that = this;
			$('#register').bootstrapValidator('validate');
			var b = $('#register').data("bootstrapValidator").isValid();
			if(!b)
				return;
			var data = {
				'UserName': this.info.UserName,
				'Mobile': this.info.Mobile,
				'Email': this.info.Email,
				'Password': this.info.Password,
				'Password1': this.info.Password,
				'IsActive': this.isAccept,
				'ValidateCode': this.info.ValidateCode
			};
			globalData.ajaxPost(this.url.registerUser, data, function(res) {
				if(!res.suc) {
					globalData.utils.alert(res.msg);
					return;
				}
				var data = {
					'Mobile': that.info.Mobile,
					'Email': '',
					'Password': that.info.Password,
					'url': globalData.constParam.url.baseUrl,
					'ClientId': '62ccad6d-9611-41c5-96f6-08ec0ada0f68',
					'ClientSecret': '5678'
				};
				url = "/api/home/LoginByWhitePwd";
				globalData.ajaxPost(url, data, function(res) {
					if(res.suc) {
						var  test = res.data
						test.isLogin = 1
						globalData.setStorage(globalData.constParam.param.UTOKEN, test);
						globalData.userInfo = res.data
						globalData.userInfo.isLogin = 1
					} else{
						that.err = true;
					}
					globalData.utils.alert('注册成功，即将跳转到法律申明界面');
					$(document).on('click', '#alert', function() {
						globalData.utils.setUrl("html/legal.html");
					})
					setTimeout(function() {
						globalData.utils.setUrl("html/legal.html");
					}, 2000)
				});
				
			});

		}
	}
})

var register_valid_json = {
	feedbackIcons: {
		valid: 'glyphicon glyphicon-ok',
		invalid: 'glyphicon glyphicon-remove',
		validating: 'glyphicon glyphicon-refresh'
	},
	fields: {
		ValidateCode: {
			validators: {
				notEmpty: {
					message: '验证码不能为空'
				}
			},
		},
		Mobile: {
			validators: {
				notEmpty: {
					message: '手机号不能为空'
				},
				stringLength: {
					min: 11,
					max: 11,
					message: '请输入11位手机号'
				},
			}
		},
		UserName: {
			validators: {
				notEmpty: {
					message: '用户名不能为空'
				},
			}
		},
		Password: {
			validators: {
				notEmpty: {
					message: '密码不能为空'
				},
				stringLength: {
                    min: 6,
                    message: '密码长度不少于6'
                },
			}
		},
		Password_2: {
			validators: {
				notEmpty: {
					message: '确认密码不能为空'
				},
				stringLength: {
                    min: 6,
                    message: '密码长度不少于6'
                },
				identical: {
					field: 'Password',
					message: "两次输入的密码不一致"
				}
			}
		},
		Email: {
			validators: {
				notEmpty: {
					message: '邮件不能为空'
				},
				emailAddress: {
					message: '请输入正确的邮件地址如：123@qq.com'
				}
			}
		},
		accept: {
			validators: {
				notEmpty: {
					message: '请接收条款'
				}
			}
		}
	}
}